package com.xiaogang.ggfw.utils.convert;

import org.ofdrw.converter.ConvertHelper;
import org.ofdrw.converter.ImageMaker;
import org.ofdrw.converter.PdfboxMaker;
import org.ofdrw.reader.OFDReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * @ProjectName : ggfw
 * @作者 : 侯小刚
 * @描述 :
 * @创建日期 : 2023/8/9 15:39
 */
public class OfdConvertUtil {

    /**
     * 将base64转换为InputStream
     * @param base64String
     * @return
     */
    public static InputStream base64ToInputStream(String base64String) {
        byte[] bytes = Base64.getDecoder().decode(base64String);
        return new ByteArrayInputStream(bytes);
    }


    /**
     * 将ofd转换为图片
     * @param inputStream 源文件流
     */
    public static List<String> ofdToImg(InputStream inputStream) {
        //创建转换转换对象，设置 每毫米像素数量(Pixels per millimeter)
        try(OFDReader reader = new OFDReader(inputStream)) {
            ImageMaker imageMaker = new ImageMaker(reader, 15);
            List<String> imageBase64List = new ArrayList<>();
            for (int i = 0; i < imageMaker.pageSize(); i++) {
                // 4. 指定页码转换图片
                BufferedImage image = imageMaker.makePage(i);
                // 将image转成base64
                String s = bufferedImageToBase64(image);
                imageBase64List.add(s);
            }
            return imageBase64List;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将图片转换为base64
     * @param image
     * @return
     */
    public static String bufferedImageToBase64(BufferedImage image) {
        String base64String = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", bos);
            byte[] imageBytes = bos.toByteArray();
            base64String = Base64.getEncoder().encodeToString(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return base64String;
    }

    /**
     * 将
     * @param inputStream
     * @return
     */
    public static String ofdToPdf(InputStream inputStream) {
        OutputStream outputStream = new ByteArrayOutputStream();
        try {
            ConvertHelper.toPdf(inputStream, outputStream);
            String base64String = outputStreamToBase64(outputStream);
            return base64String;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将输出流转换为base64
     * @param outputStream
     * @return
     */
    public static String outputStreamToBase64(OutputStream outputStream) {
        String base64String = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) outputStream;
            byte[] bytes = byteArrayOutputStream.toByteArray();
            base64String = Base64.getEncoder().encodeToString(bytes);
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        return base64String;
    }


}
